package db

import "log"

// 新建单台执行日志的记录
func (m *MysqlStruct) NewJobInstances(j *JobInstance) error {
	//tx := m.Db.Begin()
	tx := m.Db
	if err := tx.Create(&j).Error;err!=nil{
		//tx.Rollback()
		return err
	}
	//tx.Commit()
	return nil
}
// 更新记录fugai式
func (m *MysqlStruct) UpdateJobInstancesByJobIdANdInstance(jobId uint,instance string,updates map[string]interface{}) error {
	//db := m.Db.Begin()
	db := m.Db
	obj := JobInstance{}
	err := m.GetJobInstancesByJobIdANdInstance(jobId,instance,&obj)
	if err != nil {
		log.Printf("未查找到相关的记录 jobid:%d instance:%s",jobId,instance)
		return err
	}
	if err = db.Model(&obj).Updates(updates).Error;err != nil{
		//db.Rollback()
		return err
	}
	//db.Commit()
	return nil
}

// 查询指定的一条主机任务记录
func (m *MysqlStruct)GetJobInstancesByJobIdANdInstance(jobId uint,instance string,j *JobInstance)  error {
	// var result JobInstance
	tx := m.Db
	return tx.Where("job_id=? and  instance=?",jobId,instance).First(j).Error

}

//依据Jid查询所有的主机任务记录
func (m *MysqlStruct)GetJobInstancesByJobId(jobId uint ,js *[]JobInstance) error {
	tx := m.Db
	return tx.Where("job_id = ?",jobId).Find(js).Error

}
